package com.blb.eduuserservice.config;

import com.blb.common.entity.User;
import com.blb.common.util.JwtUtils;
import com.blb.common.util.RsaUtils;
import com.blb.eduuserservice.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Token请求验证过滤器
 */
//@Slf4j
//public class TokenAuthenticationFilter extends BasicAuthenticationFilter {
//
//    public static final String HEADER_NAME = "Authorization";
//
//    public TokenAuthenticationFilter(AuthenticationManager authenticationManager) {
//        super(authenticationManager);
//    }
//
//    /**
//     * 对请求进行过滤
//     */
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
//        //获得请求头
//        String token = request.getHeader(HEADER_NAME);
//        if(token == null){
//            log.info("没有token，被拦截");
//            //放行,不给通行证，请求会被拦截
//            chain.doFilter(request,response);
//            return;
//        }
//        try {
//            //解析token
//            User user = JwtUtils.getUserFromToken(token, RsaUtils.publicKey);
//            //创建通行证
//            UsernamePasswordAuthenticationToken authenticationToken
//                    = new UsernamePasswordAuthenticationToken(user.getUsername(),"",null);
//            //交给Security
//            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//        }catch (Exception ex){
//            log.error("解析token失败",ex);
//        }
//        chain.doFilter(request,response);
//    }
//}
